package com.zlz.mapreduce.joinPre;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zlz
 * @date: 2022/1/3 12:33
 * @description:
 * @since: JDK1.8
 */
public class TableReducer extends Reducer<Text, TableBean, TableBean, NullWritable> {
    @Override
    protected void reduce(Text key, Iterable<TableBean> values, Context context) throws IOException, InterruptedException {
        List<TableBean> orders = new ArrayList<>();
        TableBean productBean = new TableBean();
        for (TableBean value : values) {
            if (value.getTableName().equals(EnumTable.ORDER.getName())) {
                TableBean temp = new TableBean();
                try {
                    BeanUtils.copyProperties(temp, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                orders.add(temp);
            } else {
                try {
                    BeanUtils.copyProperties(productBean, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        for (TableBean order : orders) {
            order.setPname(productBean.getPname());
            context.write(order, NullWritable.get());
        }
    }
}
